package com.reremouse.rerebbs.web.mb;

import com.reremouse.rerebbs.dao.IImgDAO;
import com.reremouse.rerebbs.model.BbsFileModel;
import com.reremouse.rerebbs.model.BbsUser;
import com.reremouse.rerebbs.model.FileSaveStatus;
import com.reremouse.service.ImageService;
import com.reremouse.util.JsfHelper;
import com.reremouse.util.SpringHelper;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Date;

@ManagedBean
@ViewScoped
public class NewFile
        extends NewFileAbstract {
    FileUpload fu = new FileUpload();
    FileSaveStatus stat = new FileSaveStatus();

    public FileSaveStatus getStat() {
        return this.stat;
    }

    public void setStat(FileSaveStatus stat) {
        this.stat = stat;
    }

    public FileUpload getFu() {
        return this.fu;
    }

    public void setFu(FileUpload fu) {
        this.fu = fu;
    }

    @PostConstruct
    public void init() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        this.idt = request.getParameter("id");

        String fid = request.getParameter("fid");
        if (fid != null) {
            this.fatherID = Long.parseLong(fid);
        } else {
            this.fatherID = 0L;
        }
        checkName();
        if ((this.idt != null) && (!"0".equals(this.idt))) {
            System.out.println("idt:" + this.idt);
            this.project2 = this.project2DAO.findClientFile(Long.parseLong(this.idt));
            this.fnTemp = this.project2.getFileName();
            this.flag = true;
        }
    }

    public void newAdd() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String scope_t = request.getParameter("scope");
        if (scope_t != null) {
            this.scope = scope_t;
        }
        this.idt = null;
        this.flag = false;
        this.project2 = new BbsFileModel();
        this.fileName = "";
        checkName();
        this.rename = false;
    }

    public void alter() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        this.idt = request.getParameter("id");
        if (this.idt != null) {
            this.project2 = this.project2DAO.findClientFile(Long.parseLong(this.idt));
            this.flag = true;
            this.fileName = this.project2.getFileName();
            this.oldName = this.fileName;
        }
        String scope_t = request.getParameter("scope");
        if (scope_t != null) {
            this.scope = scope_t;
        }
        checkName();
        this.rename = false;
    }

    public void checkName() {
        if (this.fileName.trim().equals("")) {
            this.errStr = "文件还未命名!";
            return;
        }
        if ((!this.oldName.trim().equals("")) &&
                (this.oldName.trim().equals(this.fileName.trim()))) {
            this.errStr = "";
            return;
        }
        boolean b = this.project2DAO.checkNameIfExist(this.fileName.trim(), this.fatherID);
        if (b) {
            this.errStr = "file exist,please rename it";
        } else {
            this.errStr = "";
        }
    }

    public void up_action(FileUploadEvent event) {
        try {
            UploadedFile item = event.getFile();
            String str = "";
            InputStream f = item.getInputstream();
            Long tl = Long.valueOf(item.getSize());
            this.project2.setRealLength(tl.longValue());

            Float k = Float.valueOf(1024.0F);
            Float m = Float.valueOf(1048576.0F);
            Float g = Float.valueOf(1.073742E+009F);
            if ((float) tl.longValue() < k.floatValue()) {
                str = tl.toString() + "B";
            } else if ((k.floatValue() <= (float) tl.longValue()) && ((float) tl.longValue() < m.floatValue())) {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / k.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "KB";
            } else if (((float) tl.longValue() >= m.floatValue()) && ((float) tl.longValue() < g.floatValue())) {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / m.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "MB";
            } else {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / g.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "GB";
            }
            this.project2.setFileSize(str);
            String n = item.getFileName();
            int l1 = n.lastIndexOf("\\");
            int l2 = n.lastIndexOf(".");
            String ext = "." + n.substring(l2 + 1).toLowerCase();
            String name = n.substring(l1 + 1, l2);
            if (this.fileName.equals("")) {
                setFileName(name);
            }
            this.project2.setFileExt(ext);

            long fn = this.project2.getId();
            ClientSession cs = (ClientSession) JsfHelper.getBean("clientSession");


            this.stat.setTotal(tl.longValue());
            this.project2DAO.saveFile(f, fn, cs.getUsr().getId().longValue(), this.stat);
            this.project2.setUploadTime(new Date());
            System.out.println("upload executed.");
            checkName();
            if (!this.errStr.equals("")) {
                fileOk();
                this.flag = true;
                this.fileName = this.project2.getFileName();
                this.oldName = this.fileName;
            }
            IImgDAO imgDAO = (IImgDAO) SpringHelper.getSpringBean("ImgDAO");
            imgDAO.saveImg(item.getInputstream(), fn);
            if ((ext.toLowerCase().equals(".jpg")) || (ext.toLowerCase().equals(".gif")) || (ext.toLowerCase().equals(".png"))) {
                ImageService s = (ImageService) SpringHelper.getSpringBean("ImageService");
                s.transferUserImagesToRelativeDir((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(), Long.valueOf(fn));
            }
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        this.stat.reset();
    }

    private void fileOk() {
        BbsFileModel cfm = this.project2;
        cfm.setFatherID(this.fatherID);
        cfm.setIfFolder(Boolean.valueOf(false));

        ClientSession cs = (ClientSession) JsfHelper.getBean("clientSession");
        BbsUser cu = cs.getUsr();
        cfm.setUser(cu);
        cfm.setScope(this.scope);
        cfm.setFileName(this.fileName.trim());

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String t = request.getRemoteAddr();
        if (!this.flag) {
            cfm.setUploadTime(new Date());
            this.project2DAO.saveClientFileInfo(this.project2);
            this.project2.setAncestors(this.cxLogic.genAncestors(this.project2.getId()));
            this.project2DAO.updateClientFileInfo(this.project2);
        } else {
            this.project2DAO.updateClientFileInfo(this.project2);
        }
        ClientListFile lcf = (ClientListFile) JsfHelper.getBean("clientListFile");
        if (lcf != null) {
            lcf.synDB();
        }
        this.fu = new FileUpload();
    }

    public String createFile() {
        fileOk();

        this.project2 = new BbsFileModel();
        this.fileName = "";
        this.oldName = "";

        return null;
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.NewFile

 * JD-Core Version:    0.7.0.1

 */